package com.dragon.backstage.controller;

import com.dragon.backstage.domain.param.role.RoleAddParam;
import com.dragon.backstage.domain.param.role.RoleAuthUserAddBatchParam;
import com.dragon.backstage.domain.param.role.RoleAuthUserDeleteBatchParam;
import com.dragon.backstage.domain.param.role.RoleAuthUserParam;
import com.dragon.backstage.domain.param.role.RoleChangeStatusParam;
import com.dragon.backstage.domain.param.role.RoleEditParam;
import com.dragon.backstage.domain.param.role.RoleParam;
import com.dragon.backstage.domain.vo.role.RoleDetailVo;
import com.dragon.backstage.domain.vo.role.RoleVo;
import com.dragon.backstage.domain.vo.user.UserVo;
import com.dragon.backstage.service.RoleService;
import com.dragon.common.annotation.AuthPermission;
import com.dragon.common.annotation.UserOperatorLog;
import com.dragon.common.domain.vo.PaginationVo;
import com.dragon.common.enums.BusinessType;
import com.dragon.common.utils.AjaxResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 角色信息表 前端控制器
 * </p>
 *
 * @author dragon
 * @since 2024-08-01 18:05:42
 */
@RestController
@RequestMapping("/api/system/role")
@Tag(name = "角色模块接口")
public class RoleController {

    @Resource
    private RoleService roleService;


    /**
     * 角色列表
     *
     * @param param 角色搜索请求参数
     * @return 角色列表
     */
    @GetMapping("/list")
    @Operation(summary = "角色列表")
    @AuthPermission(value = "system.role.list")
    public AjaxResult<PaginationVo<RoleVo>> list(RoleParam param) {
        return roleService.roleList(param);
    }

    /**
     * 角色详情
     *
     * @param roleId 角色ID
     * @return 角色详情
     */
    @GetMapping("/detail")
    @Operation(summary = "角色详情")
    @AuthPermission(value = "system.role.detail")
    public AjaxResult<RoleDetailVo> detail(@RequestParam(name = "roleId", required = false) Long roleId) {
        return roleService.roleDetail(roleId);
    }

    /**
     * 添加角色
     *
     * @param param 添加角色参数
     * @return 添加结果
     */
    @PostMapping("/add")
    @Operation(summary = "添加角色")
    @AuthPermission(value = "system.role.add")
    @UserOperatorLog(title = "角色管理", businessType = BusinessType.INSERT)
    public AjaxResult<Void> add(@RequestBody RoleAddParam param) {
        return roleService.addRole(param);
    }

    /**
     * 编辑角色
     *
     * @param param 编辑角色参数
     * @return 编辑结果
     */
    @PutMapping("/edit")
    @Operation(summary = "编辑角色")
    @AuthPermission(value = "system.role.edit")
    @UserOperatorLog(title = "角色管理", businessType = BusinessType.UPDATE)
    public AjaxResult<Void> edit(@RequestBody RoleEditParam param) {
        return roleService.editRole(param);
    }

    /**
     * 删除角色
     *
     * @param roleId 角色ID
     * @return 删除结果
     */
    @DeleteMapping("/delete")
    @Operation(summary = "删除角色")
    @AuthPermission(value = "system.role.delete")
    @UserOperatorLog(title = "角色管理", businessType = BusinessType.DELETE)
    public AjaxResult<Void> delete(@RequestParam(value = "roleId", required = false) Long roleId) {
        return roleService.deleteRole(roleId);
    }

    /**
     * 更改角色状态
     *
     * @param param 请求参数
     * @return 更改结果
     */
    @PutMapping("/changeStatus")
    @Operation(summary = "更改角色状态")
    @AuthPermission(value = "system.role.change.status")
    @UserOperatorLog(title = "角色管理", businessType = BusinessType.ROLE_CHANGE_STATUS)
    public AjaxResult<Void> changeStatus(@RequestBody RoleChangeStatusParam param) {
        return roleService.changeStatus(param);
    }

    /**
     * 授权用户列表
     *
     * @param param 请求参数
     * @return 授权用户列表
     */
    @GetMapping("/authUserList")
    @Operation(summary = "授权用户列表")
    @AuthPermission(value = "system.role.auth.user")
    public AjaxResult<PaginationVo<UserVo>> authUserList(RoleAuthUserParam param) {
        return roleService.authUserList(param);
    }

    /**
     * 允许授权用户列表
     *
     * @param param 请求参数
     * @return 允许授权用户列表
     */
    @GetMapping("/allowAuthUserList")
    @Operation(summary = "允许授权用户列表")
    @AuthPermission(value = "system.role.allow.auth.user.list")
    public AjaxResult<PaginationVo<UserVo>> allowAuthUserList(RoleAuthUserParam param) {
        return roleService.allowAuthUserList(param);
    }

    /**
     * 批量添加授权用户
     *
     * @param param 请求参数
     * @return 批量添加结果
     */
    @PostMapping("/addAuthUserBatch")
    @Operation(summary = "批量添加授权用户")
    @AuthPermission(value = "system.role.auth.user.add.batch")
    @UserOperatorLog(title = "角色管理", businessType = BusinessType.ROLE_AUTH_USER_ADD_BATCH)
    public AjaxResult<Void> addAuthUserBatch(@RequestBody RoleAuthUserAddBatchParam param) {
        return roleService.addAuthUserBatch(param);
    }

    /**
     * 批量删除授权用户
     *
     * @param param 请求参数
     * @return 批量删除结果
     */
    @PostMapping("/deleteAuthUserBatch")
    @Operation(summary = "批量删除授权用户")
    @AuthPermission(value = "system.role.auth.user.delete.batch")
    @UserOperatorLog(title = "角色管理", businessType = BusinessType.ROLE_AUTH_USER_DELETE_BATCH)
    public AjaxResult<Void> deleteAuthUserBatch(@RequestBody RoleAuthUserDeleteBatchParam param) {
        return roleService.deleteAuthUserBatch(param);
    }

}
